package com.anjubao.parking.municipal.parkinglot.controller;

import com.anjubao.parking.municipal.common.dto.ApiCode;
import com.anjubao.parking.municipal.common.dto.ApiResult;
import com.anjubao.parking.municipal.parkinglot.dto.ParkingLotSlotDto;
import com.anjubao.parking.municipal.parkinglot.dto.ParkingLotSlotReturnDto;
import com.anjubao.parking.municipal.parkinglot.form.ParkingLotSlotForm;
import com.anjubao.parking.municipal.parkinglot.form.ParkingLotSlotScreeningForm;
import com.anjubao.parking.municipal.parkinglot.service.ParkingLotSlotService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author anjubao
 */
@RestController
@Api(value = "ParkingLotSlotController", tags = {"泊位管理接口"})
@RequestMapping("/parking-lots-slots")
public class ParkingLotSlotController {

    @Autowired
    private ParkingLotSlotService parkingLotSlotService;
    private static final Logger logger = LoggerFactory.getLogger(ParkingLotSlotController.class);

    /**
     * 新增泊位接口
     *
     * @param parkingLotSlotForm
     * @return
     */
    @ApiOperation(value = "新增泊位接口", notes = "新增泊位数据")
    @ApiResponses(value = {
            @ApiResponse(code = 5000, message = "操作失败")
    })
    @PostMapping("")
    public ApiResult<ParkingLotSlotReturnDto> saveParkingLotSlot(@RequestBody @Valid @ApiParam(value = "泊位信息") ParkingLotSlotForm parkingLotSlotForm) {
        logger.info("新增泊位信息 {} ", parkingLotSlotForm);
        ParkingLotSlotReturnDto returnDto = parkingLotSlotService.saveParkingLotSlot(parkingLotSlotForm);
        if (returnDto==null){
            return new ApiResult(ApiCode.STATUS_INTERNAL_ERROR.getCode(),"泊位编号已存在");
        }
        return ApiResult.success(returnDto);
    }

    /**
     * 删除泊位接口
     *
     * @param code
     * @return
     */
    @ApiOperation(value = "停车位记录删除接口", notes = "删除停车位数")
    @ApiResponses(value = {
            @ApiResponse(code = 5000, message = "操作失败")
    })
    @DeleteMapping("/{code}")
    public ApiResult<ParkingLotSlotReturnDto> deleteParkingLotSlotByCode(@PathVariable @ApiParam(value = "泊位编号") String code) {
        logger.info("删除停车位信息 {} ", code);
        ParkingLotSlotReturnDto returnDto = parkingLotSlotService.deleteParkingLotSlotByCode(code);
        return ApiResult.success(returnDto);
    }

    /**
     * 更新停车位数据接口
     * @param parkingLotSlotForm
     * @return
     */
    @ApiOperation(value = "更新泊位接口", notes = "更新泊位数据")
    @ApiResponses(value = {
            @ApiResponse(code = 5000, message = "操作失败")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "泊位编号", dataType = "String"),
    })
    @PutMapping("/{code}")
    public ApiResult<ParkingLotSlotReturnDto> updateParkingLotSlots(@PathVariable String code, @RequestBody @Valid @ApiParam(value = "泊位信息") ParkingLotSlotForm parkingLotSlotForm) {
        logger.info("更新泊位信息 {} ", parkingLotSlotForm);
        ParkingLotSlotReturnDto returnDto = parkingLotSlotService.updateParkingLotSlotByCode(code, parkingLotSlotForm);
        if (returnDto==null){
            return new ApiResult<>(ApiCode.STATUS_INTERNAL_ERROR.getCode(), "泊位编号已存在");
        }
        return ApiResult.success(returnDto);
    }



    /**
     * 获取泊位数据,并分页
     * @param page
     * @param pageSize
     * @param form
     * @return
     */
    @ApiOperation(value = "搜索泊位列表接口", notes = "获取泊位数据")
    @ApiResponses(value = {
            @ApiResponse(code = 5000, message = "操作失败")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页号",dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "页大小",dataType = "int"),
    })
    @GetMapping("")
    public ApiResult<List<ParkingLotSlotDto>> getParkingLotSlots(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize,
                                                                 ParkingLotSlotScreeningForm form) {
        logger.info("获取泊位分页数据 {} ",page,pageSize,form);
        Page<ParkingLotSlotDto> parkingLotSlots = parkingLotSlotService.getParkingLotSlots(page, pageSize, form);
        return ApiResult.success(parkingLotSlots.getRecords(),(int)parkingLotSlots.getTotal());
    }

    /**
     * 根据泊位编号查询泊位记录
     * @param code
     * @return
     */
    @ApiOperation(value = "单个泊位记录获取接口", notes = "根据泊位编号获取泊位记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "泊位编号", dataType = "String"),
    })
    @ApiResponses(value = {
            @ApiResponse(code = 5000, message = "操作失败")
    })
    @GetMapping("/{code}")
    public ApiResult<ParkingLotSlotDto> getParkingLotSlotByCode(@PathVariable String code) {
        ParkingLotSlotDto parkingLotSlotDto = parkingLotSlotService.getParkingLotSlotByCode(code);
        return ApiResult.success(parkingLotSlotDto);
    }

    /**
     * 根据所属车场code获取泊位列表
     *
     * @param parkingLotCode
     * @return
     */

    @ApiOperation(value = "获取泊位编号列表", notes = "根据所属车场编号获取泊位列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parkingLotCode", value = "车场编号", dataType = "String"),
            @ApiImplicitParam(name = "type", value = "设备类型 1:充电桩,2:视频桩,3:地磁,4:pda,5:诱导屏", dataType = "int"),
    })
    @ApiResponses(value = {
            @ApiResponse(code = 5000, message = "操作失败")
    })
    @GetMapping("/by-parking-lot-code")
    public ApiResult<List<ParkingLotSlotDto>> getSlotsByParkingLotCode(@RequestParam String parkingLotCode,@RequestParam Integer type) {
        List<ParkingLotSlotDto> parkingLotSlots = parkingLotSlotService.getSlotsByParkingLotCode(parkingLotCode,type);
        return ApiResult.success(parkingLotSlots,parkingLotSlots.size());
    }

}
